package logic

import (
	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
	"iot-base/auth/api/logic/platformmanager"
	"iot-base/auth/config"
	"iot-base/auth/logger"
	"iot-base/auth/model"
	"iot-base/auth/svc"
	"iot-base/auth/types"
	"iot-base/auth/utils"
)

func DelPlatform(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types.NewResult(c)
	var del types.ReqDelPlatform
	if err := c.ShouldBind(&del); err != nil {
		logger.Log.Errorf("delplatform get [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	if err := svc.Dao.TPlatform.DeleteById(del.ID); err != nil {
		logger.Log.Error("delete platform db err")
		resp.ErrorCode(types.DBError, "")
		return
	}
	platformmanager.DelPlatform(del.ID)
	resp.Success(gin.H{"result": "ok"})
}

func ModifyPlatform(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types.NewResult(c)
	var platform model.Platform
	if err := c.ShouldBind(&platform); err != nil {
		logger.Log.Errorf("platform get [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	if err := platformmanager.UpdatePlatform(platform); err != nil {
		logger.Log.Errorf("add platform err : %v", err)
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	data, _ := utils.StructToMap(platform)
	if err := svc.Dao.TPlatform.Update(platform.ID, data); err != nil {
		logger.Log.Errorf("db creat [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.Fail, "db creat err")
		return
	}
	resp.Success(gin.H{"result": "ok"})
}

//增加上传平台
func AddPlatform(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types.NewResult(c)
	var platform model.Platform
	if err := c.ShouldBind(&platform); err != nil {
		logger.Log.Errorf("platform get [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	if err := platformmanager.UpdatePlatform(platform); err != nil {
		logger.Log.Errorf("add platform err : %v", err)
		resp.ErrorCode(types.Fail, err.Error())
	}
	if err := svc.Dao.TPlatform.Add(&platform); err != nil {
		logger.Log.Errorf("db creat [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.DBError, "db creat err")
		return
	}
	resp.Success(gin.H{"result": "ok"})
}

func ListPlatform(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types.NewResult(c)
	platforms, err := svc.Dao.TPlatform.FindAll()
	if err != nil {
		logger.Log.Errorf("db query [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	resp.Success(platforms)
}

func PlatformStyle(c *gin.Context) {
	resp := types.NewResult(c)
	var result []string
	for k, _ := range platformmanager.ProtocolMap {
		result = append(result, k)
	}
	resp.Success(gin.H{"result": result})
}

func init() {
	models, _ := svc.Dao.TTModel.WhereInId([]string{"m_nox", "m_co", "m_so2", "m_o3", "m_pm25", "m_pm10"})
	jStr := ``
	for c, v := range models {
		jStr += `"` + v.UID + `"` + `:` + `[`
		body := v.Body.String()
		properties := gjson.Get(body, "properties").Array()
		for n, p := range properties {
			jStr += `"` + p.Get("identifier").String() + `"`
			if len(properties) != n+1 {
				jStr += `,`
			}
		}
		jStr += `]`
		if c != len(models)+1 {
			jStr += `,`
		}
	}
	config.UpdateJson(jStr)
}

func InitPlatform() {
	platformmanager.InitPlatform()
}
